tongfusoft.docDetail = function() {
}

tongfusoft.docDetail.prototype = {

	
	returnBtn : function() {

		$("#returnBtn").click(function() {

			window.location.href = common.getContextPath($("#returnPage").val());
		})

	},
	getDocDetail : function() {
		common.post('docDetail/view/' + $("#docName").val()+'?docId='+$("#docId").val(), {}, function(data) {

			if (data.state == 'success') {
				if(data.data.docData!=null){
					$.map(data.data.docData, function(v, k) {
						$("#" + k).text(v);

					})
					
					$.map(eval('(' + data.data.docData.docMetadata + ')'), function(v1, k1) {
						$("#" + k1).text(v1);

					})
				
					$("#docFile").html('<a href="'+common.getContextPath('download/'+data.data.docData.docName+'?docId='+(data.data.docData.docId==null?'':data.data.docData.docId))+'" target="_blank" >'+data.data.docData.originalName+'</a>')
				}
				
				if(data.data.docHistory!=null){
					var docHistoryHtml='';
					$.each(data.data.docHistory,function(index,item){
						
						docHistoryHtml=docHistoryHtml+'<tr>';
						
						if(item.docVersion==data.data.docData.docVersion){
							
							docHistoryHtml=docHistoryHtml+'<td>['+item.docVersion+']</td>';

						}else{
							
							var url=window.location.pathname.replace( $("meta[name='_ctx']").attr("content"),"").substr();
							docHistoryHtml=docHistoryHtml+'<td><a href="'+common.getContextPath(url.substr(1,url.length )+'?docId='+(item.docId==null?'':item.docId))+'" >'+item.docVersion+'</a></td>';

							
						}
						docHistoryHtml=docHistoryHtml+'<td><a href="'+common.getContextPath('download/'+item.docName+'?docId='+(item.docId==null?'':item.docId))+'" target="_blank" >'+item.originalName+'</a></td>';
						docHistoryHtml=docHistoryHtml+'<td>'+item.lastUpdDate+'</td>';
						
						if(item.docId!=null&&item.docId!=''){
							docHistoryHtml=docHistoryHtml+'<td><a href="javascript:void(0)" docName="'+item.docName+'" docId="'+item.docId+'" onClick="new tongfusoft.docDetail().deleteBtn(this)"  >删除</a></td>';

							
						}else{
							
							docHistoryHtml=docHistoryHtml+'<td></td>';

						}


						docHistoryHtml=docHistoryHtml+'</tr>';
						
					});
					$("#historyData").html(docHistoryHtml);
				}
			}

		});

	},
	//删除
	deleteBtn:function(obj){
		
		var url='docDel/'+$(obj).attr("docname")+'?docId='+$(obj).attr("docId")
		
		common.post(url, {}, function(data) {

			if (data.state == 'success') {
				swal({
					title : "是否确认删除?",
					text : "",
					type : "warning",
					showCancelButton : true,
					confirmButtonColor : "#DD6B55",
					confirmButtonText : "删除",
					cancelButtonText : "取消",
					closeOnConfirm : true
				}, function() {
					
					if(data.code=='detail_null'){
						window.location.href = common.getContextPath('mydoc');
						
					}else{
							window.location.reload();
					}
				});
			
				
			}else{
				
				toastr["error"]('操作权限不足');
				
			}
		});

				
	},
	//收藏
	userFavorite : function() {
		common.post('isUserFavorite', {
			"docName" : $("#docName").val()
		}, function(data) {
			var favoriteHtml = "";
			if (data.state == 'success') {
				favoriteHtml = '<button type="button" class="btn btn-default fa fa-star mr-sm"   id="delFavoriteBtn"> 收藏</button>';
			}else{
				favoriteHtml = '<button type="button" class="btn btn-default fa fa-star-o mr-sm"   id="addFavoriteBtn"> 收藏</button>';
			}
			$("#userFavorite").html(favoriteHtml);
		});
		$("#addFavoriteBtn").click(function() {
			common.post('createUserFavorite', {
				"docName" : $("#docName").val()
			}, function(data) {
				
				if (data.state == 'success') {
					new tongfusoft.docDetail().userFavorite();
				}
			});
		});
		$("#delFavoriteBtn").click(function() {
			common.post('deleteFavoriteBtn', {
				"docName" : $("#docName").val()
			}, function(data) {
				if (data.state == 'success') {
					new tongfusoft.docDetail().userFavorite();
				}
				
			});
		})
	},
	//关注
	concernBtn : function() {
		var currUserId = "";
		common.post('currUser', {
		}, function(data) {
			if (data.state == 'success') {
				currUserId = data.data.userId;
			}
		});
		common.post('isUserFollower', {
			"docName" : $("#docName").val(),
			"createdBy":currUserId
		}, function(data) {
			var concernHtml = "";
			if (data.state == 'success') {
				concernHtml = '<button type="button" class="btn btn-default fa fa-star mr-sm"   id="delUserFollowBtn"> 关注</button>';
			}else{
				concernHtml = '<button type="button" class="btn btn-default fa fa-star-o mr-sm"   id="addUserFollowBtn"> 关注</button>';
			}
			$("#userFollower").html(concernHtml);
		});
		
		$("#addUserFollowBtn").click(function() {
			var url='userFollows/add/'+$("#docName").val()
			common.ajaxSubmitOnSuccess({},url , function(data) {

				if(data.state == 'success'){
					new tongfusoft.docDetail().concernBtn();
				}
			})
		})
		$("#delUserFollowBtn").click(function() {
			var url='userFollows/del/'+$("#docName").val()
			common.ajaxSubmitOnSuccess({},url , function(data) {

				if(data.state == 'success'){
					new tongfusoft.docDetail().concernBtn();
				}

			})
		})
	}
}

$(function() {
	var obj = new tongfusoft.docDetail();
	obj.concernBtn();
	obj.userFavorite();
	obj.returnBtn();
	obj.getDocDetail();
})